import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;

import momo.app.util.IntByteConvert;

public class EchoServer
{
	private static String getTextContents(String filename)
	{
		File f = new File(filename);
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
			String line = "";
			String lineSep = System.getProperty("line.separator");
			while( (line=in.readLine()) != null )
			{
				buffer.append(line);
				buffer.append(lineSep);
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	
	public static void main(String args[])
	{
		String data = getTextContents("./trial_2.txt");
		try {
			ServerSocket server = new ServerSocket(1111);
			Socket client = server.accept();
			
			BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
			System.out.println("Send Size: " + client.getSendBufferSize());
			
			while(true)
			{
				try
				{
					Thread.sleep(1 * 1);
				}catch (Exception e) {
					e.printStackTrace();
				}
				byte[] b = IntByteConvert.dataToByteArray(data);
				out.write(b);
				
//				out.write(data.getBytes());
				out.flush();
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
//	public static void main(String args[])
//	{
//		File f = new File("./trial_2.txt");
//		FileWriter out;
//		try {
//			out = new FileWriter(f);
//			String data = "1234567890abcdefghijklmnopqrstuvwxyz\n";
//			for(int i=1; i<=1000; i++)
//			{
//				out.write(i+"  " + data);
//			}
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
